package com.xiechuang.helper;

import java.util.regex.Pattern;

/**
 * @Description: 正则助手
 * @Author: xiechuang
 * @Date: 2023/1/11
 */
public class RegexHelper {

    // Empty
    public static final Pattern emptyRegex = Pattern.compile("");

    // 数字
    public static final Pattern numberRegex = Pattern.compile("^-?[0-9]+$");

    // 字母
    public static final Pattern onlyLetterRegex = Pattern.compile("^[a-zA-Z]+$");

    // 包含数字以及横线
    public static final Pattern onlyCharAndNumberRegex = Pattern.compile("^[a-zA-Z0-9]+([-_][a-zA-Z0-9]+)*$");

    // 同上在加中文
    public static final Pattern onlyCharAndNumberAndChRegex = Pattern.compile("^[a-zA-Z0-9\u4e00-\u9fa5\\s]+([-_][a-zA-Z0-9\u4e00-\u9fa5\\s]+)*$");

    // 邮箱
    public static final Pattern emailRegex = Pattern.compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.[a-z]+([-.][a-z]+)*$");

    // 手机号
    public static final Pattern mobileRegex = Pattern.compile("^1\\d{10}$", Pattern.CASE_INSENSITIVE);

    // 图片后缀
    public static final Pattern imagePostfixRegex = Pattern.compile("\\.(?:GIF|JPG|JPEG|BMP|PNG)$", Pattern.CASE_INSENSITIVE);

    // 日期
    public static final Pattern dateRegex = Pattern.compile("^\\d{4}(\\/|\\-|年)\\d{1,2}(\\/|\\-|月)\\d{1,2}.?(\\s\\d{1,2}.\\d{1,2}(.\\d{1,2})?)?$");

    // 时间戳类型日期
    public static final Pattern numberDateRegex = Pattern.compile("^\\d{12,13}$");

    // 日期
    public static final Pattern dateRegexSimple = Pattern.compile("^\\d{4}.\\d{1,2}.\\d{1,2}.?$");

    // 网址
    public static final Pattern urlRegex = Pattern.compile("^http:\\/\\/([\\w-]+(\\.[\\w-\\/]+)+)+$");

    // 标签
    public static final Pattern tagRegex = Pattern.compile("^\\s*(([a-z0-9A-Z\\u4e00-\\u9fa5]{2,8})\\s+){0,2}([a-z0-9A-Z\\u4e00-\\u9fa5]{2,8}\\s*)?$");

    // 中文
    public static final Pattern chineseRegex = Pattern.compile("[\u4e00-\u9fa5]+");

    // URL 图片缩放尺寸
    public static final Pattern urlWidth = Pattern.compile(".+\\!(\\d+)$");
    public static final Pattern urlHeight = Pattern.compile(".+\\!h(\\d+)$");
    public static final Pattern urlWidthAndHeight = Pattern.compile(".+\\!(\\d+)x(\\d+)$");

    // 占位符
    public static final Pattern placeholder = Pattern.compile("\\$\\{([^{}]+)\\}");

    // SQL 注入
    public static final Pattern sqlInjector = Pattern.compile("where\\s|select\\s|update\\s|insert\\s|sleep|\\(|\\)");

    // XSS 注入
    public static final Pattern xssInjector = Pattern.compile("script|javascript");

}
